<?php #$oPassword = new PasswordRecovery();  ?>
<div class="row">
    <div class="col-md-4 col-md-offset-4">
        <div class="login-panel panel panel-default" id="div_form">
            <div class="panel-heading">
                <h3 class="panel-title">Bienvenido <?php echo $oPassword->getUsuario()->getFullName(); ?> </h3>
            </div>
            <div class="panel-body">
                <form id="update_form" 
                      data-action_url="<?php echo url_for("login/doUpdatePassword"); ?>" 
                      data-success_url="<?php echo url_for("login/index"); ?>" role="form"
                      data-user_id="<?php echo $oPassword->getIdUsuario(); ?>">
                    <fieldset>
                        <div class="form-group">
                            <input id="password1" class="form-control" placeholder="Nueva Contraseña" type="password" autofocus>
                        </div>
                        <div class="form-group">
                            <input id="password2" class="form-control" placeholder="Confirme Contraseña" type="password" value="">
                        </div>
                        <div class="form-group">
                            <button type="button" id="submit_change" class="btn btn-outline btn-success">
                                <span class="glyphicon glyphicon-user"></span> Actualizar Password
                            </button>
                            <button type="reset" class="btn btn-outline btn-danger">
                                <span class="glyphicon glyphicon-remove"></span> Cancelar
                            </button>
                        </div>
                    </fieldset>
                </form>
            </div>
        </div>
    </div>
</div>
<script>        
    $(document).ready(function() {
        $("#submit_change").on("click", function(event) {
            var pass1 = $("#password1").val();
            var pass2 = $("#password2").val();
            if (pass1 != pass2) {
                jsUtil.Gritter.addErrorMessage("Password deben ser identicas");
            } else {
                $.ajax({
                    url: $("#update_form").data("action_url"),
                    type: 'post',
                    dataType: 'json',
                    data : {
                        "user_id": $("#update_form").data("user_id"),
                        "new_passwd": pass1
                    },
                    success: function (oData) {
                        if (oData.message_list.length == 0) {
                            jsUtil.Gritter.addSuccessMessage("Password Actualizada Correctamente!");
                            setTimeout(function() {
                                window.location = $("#update_form").data("success_url");
                            }, 3000);
                        } else {
                            for (var i = 0; i <= oData.message_list.length; i++) {
                                jsUtil.Gritter.addErrorMessage(oData.message_list[i]);
                            }
                        }
                    }
                });
            }
        });        
    });
</script>